//index.js
//获取应用实例
const app = getApp()
var tabs = [
  {
    name: "拼车"
  },
  {
    name: "包车"
  },
  {
    name: "面对面"
  }

];
var dateTimePicker = require('../../utils/dataTimePicker.js');
Page({
  /**
    * 页面的初始数据
    */
  data: {
    //模式选择声明变量
    items: [
      { value: 'in', name: '进站', checked: 'true' },
      { value: 'out', name: '出站' },
    ],
    list:[
      {
        price:5,
        num:1,
        bgcolor:'#fff',
        self:false,
      },
      {
        price: 5,
        num: 1,
        bgcolor: '#fff',
        self: false,
      },
      {
        price: 5,
        num: 1,
        bgcolor: '#fff',
        self: false,
      },
      
    ],
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050,
    tabs: tabs,    //展示的数据
    slideOffset: 0,//指示器每次移动的距离
    activeIndex: 0,//当前展示的Tab项索引
    sliderWidth: 96,//指示器的宽度,计算得到
    contentHeight: 0,//页面除去头部Tabbar后，内容区的总高度，计算得到
    trainbox: ['01', '02', '03', '04', '05', '06', '07', '08'],//定义车厢
    trainIndex: 0,//定义车厢初始
    starPlace: ['东进站口', '西进站口', '南进站口', '北进站口', '服务台',''],//定义地点内容
    starIndex: 0,//定义地点初始值
    isChecked: false,//定义按钮选择状态
    num: 1,//input的默认值是1
    minusStatus: 'disabled', // 使用data数据对象设置样式名


  },

  // radio事件
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)

    var items = this.data.items;
    for (var i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }

    this.setData({
      items: items
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          //计算相关宽度
          sliderWidth: res.windowWidth / that.data.tabs.length,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
          contentHeight: res.windowHeight - res.windowWidth / 750 * 68//计算内容区高度，rpx -> px计算
        });
      }
    });

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },
  changeDate(e) {
    this.setData({ date: e.detail.value });
  },
  changeTime(e) {
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e) {
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e) {

    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  bindChange: function (e) {
    var current = e.detail.current;
    this.setData({
      activeIndex: current,
      sliderOffset: this.data.sliderWidth * current
    });
    console.log("bindChange:" + current);
  },

  navTabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log("navTabClick:" + e.currentTarget.id);
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // 数量加减
  /* 点击减号 */
  bindMinus: function (e) {
    // console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var num = list[index].num;
    console.log(list)
    if (list[index].self == false) {
      return
    }
    // var num = that.data.num;
    // // 如果大于1时，才可以减
    if (num > 1) {
      num--;
    }
    list[index].num = num;
    // // 只有大于一件的时候，才能normal状态，否则disable状态
    // var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // // 将数值与状态写回
    that.setData({
      // num: num,
      list:list
      // minusStatus: minusStatus
    });
  },
  
  /* 点击加号 */
  bindPlus: function (e) {
    console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    if (list[index].self == false){
      return
    }
    var num = list[index].num
    // // 不作过多考虑自增1
    num++;
    list[index].num = num
    // // 只有大于一件的时候，才能normal状态，否则disable状态
    // var minusStatus = num < 1 ? 'disabled' : 'normal';
    // // 将数值与状态写回
    that.setData({
      list: list,
      // minusStatus: minusStatus
    });
  },
  /* 点击减号 */
  bindMinus2: function (e) {
    // console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var num = list[index].num;
    console.log(list)
    if (list[index].self == false) {
      return
    }
    // var num = that.data.num;
    // // 如果大于1时，才可以减
    if (num > 1) {
      num--;
    }
    list[index].num = num;
    // // 只有大于一件的时候，才能normal状态，否则disable状态
    // var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // // 将数值与状态写回
    that.setData({
      // num: num,
      list:list
      // minusStatus: minusStatus
    });
  },
  
  /* 点击加号 */
  bindPlus2: function (e) {
    console.log(e)
    var that = this;
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    if (list[index].self == false){
      return
    }
    var num = list[index].num
    // // 不作过多考虑自增1
    num++;
    list[index].num = num
    // // 只有大于一件的时候，才能normal状态，否则disable状态
    // var minusStatus = num < 1 ? 'disabled' : 'normal';
    // // 将数值与状态写回
    that.setData({
      list: list,
      // minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回
    this.setData({
      num: num
    });
  },


  // 车厢选择
  bindTrainChange: function (e) {
    var that = this;
    that.setData({
      trainIndex: e.detail.value
    })
  },
  //出发地点选择
  bindStarPlace: function (e) {
    var that = this;
    that.setData({
      starIndex: e.detail.value
    })
  },
  //行李大小选择按钮
  changeColor: function (e) {
    var that = this;
    // that.setData({
    //   isChecked: true,
    // })
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    // if(list[index].self){
    //   that.self = false;
    //   that.bgcolor = '#ccc';
    // }else{
    //   that.self = true;
    //   that.bgcolor = '#ff9800';
    // }
    for(var i in list){
      list[i].num = 1
      list[i].bgcolor='#ccc'
      list[i].self = false
    }
    list[index].bgcolor = '#ff9800'
    list[index].self = true
    that.setData({
      list: list
    })
  },
  // 页面跳转
  bindqryy:function(){
    wx.navigateTo({
      url: '../../pages/qryy/qryy',
    })
  },
})
